#define LED 8
#define FEN 7

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX=2,TX=3
int L = 0;
int F = 5;
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(LED,OUTPUT);
  pinMode(FEN,OUTPUT);
  digitalWrite(LED, LOW);
  analogWrite(FEN, 0);
}

void loop() {
  int a = analogRead(A0);
  int b = analogRead(A1);
  Serial.print("温度:");
  Serial.println(a);
  Serial.print("光照:");
  Serial.println(b);
  Serial.print("L:");
  Serial.println(L);
  Serial.print("F:");
  Serial.println(F);//*/
  if(L == 0)
  {
    setLED(b, 600);
  }
  if(F == 5)
  {
    setFEN(b, 500);
  }
  mySerial.listen();
  if(mySerial.available() > 0) 
  {
    char s = mySerial.read();
    Serial.write(s);
    setS(s);
  }
  delay(1000);
}
void setLED(int a,int f)
{
  if(a >= f)
  {
    Serial.print("亮度低");
    digitalWrite(LED, HIGH);
  }
  else
  {
    Serial.print("亮度高");
    digitalWrite(LED, LOW);
  }
}
void setFEN(int a,int f)
{
  if(a <= f)
  {
    analogWrite(FEN, 1024);
  }
  else
  {
    analogWrite(FEN, 0);
  }
}
void setS(char Sin)
{
  switch(Sin)
  {
    case '2': //关闭
      digitalWrite(LED, LOW);
      L = 2;
      break;
    case '1': //开启
      digitalWrite(LED, HIGH);
      L = 1;
      break;
    case '0': //自动
      L = 0;
      break;
    case '3': //关闭
      F = 3;
      break;
    case '4': //开启
      F = 4;
      break;
    case '5': //自动
      F = 5;
      break;
  }
}
